package com.yeqf.demo

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.yeqf.expandrecyclerview.Adapter.OnClickListener
import com.yeqf.expandrecyclerview.R
import com.yeqf.expandrecyclerview.Adapter.RecyclerAdapter
import com.yeqf.expandrecyclerview.Dividers.LinearDecoration
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val mAdapter: RecyclerAdapter<String> = MyAdapter(this)
                .setItemLayout(R.layout.item)
                .setHeaderLayout(R.layout.header)
                .setFooterLayout(R.layout.footer)
                .setItemClickView(R.id.item_root, object : OnClickListener {
                    override fun onItemClick(v: View, position: Int) {
                        Toast.makeText(this@MainActivity, "click: $position", Toast.LENGTH_SHORT).show()
                    }

                    override fun onItemLongClick(v: View, position: Int): Boolean {
                        Toast.makeText(this@MainActivity, "longClick: $position", Toast.LENGTH_SHORT).show()
                        return true
                    }
                })

        list.adapter = mAdapter
        list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        list.addItemDecoration(LinearDecoration(this, LinearLayoutManager.VERTICAL))

        var mDatas: ArrayList<String> = ArrayList<String>()
        for (i in 0 .. 10) {
            mDatas.add("测试" + i.toString())
        }
        mAdapter.setItems(mDatas)
    }

    class MyAdapter(val context: Context) : RecyclerAdapter<String>(context) {

        override fun onBindItemDatas(holder: ViewHolder, data: String?, position: Int) {
            if(data != null) {
                holder.getView<TextView>(R.id.item_name)?.text = data
            }
        }

        override fun onBindHeaderDatas(holder: ViewHolder) {
            holder.getView<EditText>(R.id.edit)?.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {

                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    Toast.makeText(context, s.toString(), Toast.LENGTH_SHORT).show()
                }
            })
        }

        override fun onBindFooterDatas(holder: ViewHolder) {

        }
    }
}
